package com.example.netcomponent.net;


import android.os.Bundle;

import com.example.netcomponent.net.utils.LogUtils;
import com.example.netcomponent.net.utils.ToastUtils;
import com.google.gson.Gson;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.ref.SoftReference;

import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zengxianghui900 on 17/9/30.
 * 请求数据统一封装
 */

public abstract class BaseApi<T> implements Func1<BaseResultEntity<T>, T> {
    //rx生命周期管理
    private SoftReference<RxAppCompatActivity> rxAppCompatActivity;
    /*回调*/
    private HttpOnNextListener listener;
    /*是否能取消加载框*/
    private boolean cancel;
    /*是否显示加载框，内容不为空，则显示*/
    private String progressMsg;
    /*超时时间-默认60秒*/
    private int connectionTime = 60;
    /* 失败后retry次数*/
    private int retryCount = 1;
    /*失败后retry延迟*/
    private long retryDelay = 100;
    /*失败后retry叠加延迟*/
    private long retryIncreaseDelay = 10;
    /*请求是否需要携带token,也即cookie,默认需要，登录不需要,可单独设置为false*/
    private boolean isNeedToken = true;
    //是否回调进度
    private boolean showProgress = false;

    public BaseApi(RxAppCompatActivity rxAppCompatActivity) {
        setRxAppCompatActivity(rxAppCompatActivity);      //与当前activity绑定
    }

    public RxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) {
        this.rxAppCompatActivity = new SoftReference<RxAppCompatActivity>( rxAppCompatActivity);
    }

    public HttpOnNextListener getListener() {
        return listener;
    }

    public void setListener(HttpOnNextListener listener) {
        this.listener = listener;
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public String getProgressMsg() {
        return progressMsg;
    }

    public void setProgressMsg(String progressMsg) {
        this.progressMsg = progressMsg;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public long getRetryDelay() {
        return retryDelay;
    }

    public void setRetryDelay(long retryDelay) {
        this.retryDelay = retryDelay;
    }

    public long getRetryIncreaseDelay() {
        return retryIncreaseDelay;
    }

    public void setRetryIncreaseDelay(long retryIncreaseDelay) {
        this.retryIncreaseDelay = retryIncreaseDelay;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isNeedToken() {
        return isNeedToken;
    }

    public void setNeedToken(boolean needToken) {
        isNeedToken = needToken;
    }

    /**
     * 设置retrofit参数值并创建Observable对象，由子类去实现
     *
     * @param retrofit
     * @return
     */
    public abstract Observable getObservable(Retrofit retrofit);


    /**
     * 对数据进行转换处理,后面在处理时要对返回的数据进行判空操作
     * @param httpResult
     * @return
     */
    @Override
    public T call(BaseResultEntity<T> httpResult) {
//        String str = new Gson().toJson(httpResult);
        LogUtils.e("zxh","httpResult==" + new Gson().toJson(httpResult));
        //TODO: 请求成功,888是电子签名的，后续可能要改
        if ("000000".equals(httpResult.getCode()) || "888".equals(httpResult.getCode())){
            return httpResult.getData();
        }else if ("945004".equals(httpResult.getCode())){
            //存储一下请求失败后的msg
            LogUtils.saveLogToSD(LogUtils.INTERFACE_MODULE,httpResult.getMsg());
//            jump2FaceLogin();
            return null;
        }else{
            //存储一下请求失败后的msg
            LogUtils.saveLogToSD(LogUtils.INTERFACE_MODULE,httpResult.getMsg());

            RxAppCompatActivity activity = getRxAppCompatActivity();
            if (activity != null) {
                ToastUtils.showShortToast(activity,httpResult.getMsg());
            }
            HttpOnNextListener listener = this.listener;
            if (listener != null) {
                listener.onError(new HttpDataException(AppConstant.Net.NO_DATA));
            }
            return null;
        }
    }

    /**
     * 跳转到人脸识别登录界面
     */
//    private void jump2FaceLogin() {
//        RxAppCompatActivity activity = getRxAppCompatActivity();
//        if (activity != null) {
//            Bundle bundle = new Bundle();
//            bundle.putString(AppConstant.Login.FACE_LOGIN_TYPE,AppConstant.Login.TOKEN_FAILED);
//            UIRouter.getInstance().openUri(activity,"PAComp://login/faceLoginActivity",bundle,AppConstant.IntentKey.REQUEST_REFRESH_TOKEN);
//        }
//    }
}
